package com.petsdar.controller;

import com.petsdar.pojo.Task;
import com.petsdar.service.TaskService;
import com.petsdar.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created with IntelliJ IDEA
 * User: heroC
 * Date: 2020/7/2
 * Time: 19:05
 * Description: 月任务相关接口处理
 * Version: V1.0
 */
@RestController
public class TaskController {
    /** task相关的业务逻辑处理实例对象 */
    private TaskService taskService;

    @Autowired
    public void setTaskService(TaskService taskService) {
        this.taskService = taskService;
    }

    /**
     * 获取所有任务信息
     * @return
     */
    @GetMapping("/task")
    public String getTask(){
        List<Task> task = taskService.getTask();
        if (!task.isEmpty()){
            return ResultUtil.success(task);
        }
        return ResultUtil.fail();
    }

    /**
     * 添加新的任务信息
     * @param taskJson 任务信息的json数据
     * @return
     */
    @PostMapping("/task")
    public String addTask(@RequestBody String taskJson){
        boolean result = taskService.addTask(taskJson);
        if (result){
            return ResultUtil.success();
        }
        return ResultUtil.fail();
    }

    /**
     * 通过id删除任务信息
     * @param taskId 任务id
     * @return
     */
    @DeleteMapping("/task")
    public String delTask(@RequestParam("taskId") String taskId){
        boolean result = taskService.delTask(taskId);
        if (result){
            return ResultUtil.success();
        }
        return ResultUtil.fail();
    }

}
